#!/sbin/sh

# Copyright 2013 Koushik Dutta, 2014 Ron Rieve, 2017 Sergey Yeriomin
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

# arg 1 is recovery api version, generally 3.
# arg 2 is the pipe fd, to the recovery binary.
# communicate with it using the recovery api.
# arg 3 is the zip file

PACKAGE_NAME=YalpStore

echo -n -e 'ui_print Installing ${PACKAGE_NAME} as a privileged app...\n' > /proc/self/fd/$2

APK_FILENAME=${PACKAGE_NAME}.apk
ADDOND=81-${PACKAGE_NAME}.sh

cd /tmp
mkdir ${PACKAGE_NAME}
cd ${PACKAGE_NAME}
unzip -o "$3"
if [ "$?" -ne "0" ]
then
  cp /cache/${APK_FILENAME} .
  cp /cache/${ADDOND} .
fi

mount /system
rm -f /system/app/${PACKAGE_NAME}*
rm -f /system/app/${PACKAGE_NAME}*/*
rm -f /system/priv-app/${PACKAGE_NAME}*
rm -f /system/priv-app/${PACKAGE_NAME}*/*

BUILD_VERSION_SDK="$(grep -F ro.build.version.sdk /system/build.prop)"
BUILD_VERSION_SDK_INT="${BUILD_VERSION_SDK#*=}"
if [ "${BUILD_VERSION_SDK_INT}" -ge 21 ]
then
  PACKAGE_DIR="/system/priv-app/${PACKAGE_NAME}"
  mkdir ${PACKAGE_DIR}
  chmod 755 ${PACKAGE_DIR}
elif [ "${BUILD_VERSION_SDK_INT}" -ge 19 ]
then
  PACKAGE_DIR="/system/priv-app"
else
  PACKAGE_DIR="/system/app"
fi

cp ${APK_FILENAME} ${PACKAGE_DIR}
chmod 644 ${PACKAGE_DIR}/${APK_FILENAME}

cp ${ADDOND} /system/addon.d/

umount /system

echo -n -e 'ui_print done\n' > /proc/self/fd/$2
echo -n -e 'ui_print\n' > /proc/self/fd/$2